<template>
  <div class="person">
    <h1>情况三:监视[reactive]定义的【对象类型】数据</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <button @click="changeName">修改名称</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="change">修改对象</button>
    <hr>
    <h2>测试{{ obj.a.b.c }}</h2>
    <button @click="test">修改obj.a.b.c</button>
  </div>
</template>

<script lang="ts" setup>
import { reactive, watch } from "vue";

//数据
let person = reactive({
  name: '张三',
  age: 18
})

let obj = reactive({
  a: {
    b: {
      c: 'd'
    }
  }
})

function test() {
  obj.a.b.c += '2222'
}

function changeName() {
  person.name += '~'
}

function changeAge() {
  person.age += 1
}

function change() {
  Object.assign(person, {
    name: '李四',
    age: 10
  })
}

// 监视 ,情况三监视【reactive】定义的【对象类型】的数据，且默认是开启深度监视的。
watch(person, (newValue, oldValue) => {
  console.log('person变化', newValue, oldValue)
})

watch(obj, (newValue, oldValue) => {
  console.log('obj', newValue, oldValue)
})


</script>

<style scoped>
.person {
  background-color: #ddd;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}

li {
  font-size: 20px;
}

button {
  margin: 0 10px auto;
}
</style>
